//
//  EDSGuideViewManager.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/8/16.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSGuideViewManager.h"
#import "EDSGuideViewController.h"
#import <objc/runtime.h>

@implementation EDSGuideViewManager

static EDSGuideViewManager *_instance;

+ (instancetype)manager
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init];
    });
    
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    return _instance;
}

- (instancetype)copyWithZone:(NSZone *)zone
{
    return _instance;
}


- (void)begin
{
    NSAssert(self.images && self.images.count != 0, @"您没有设置图片！");
    NSAssert(self.shouldDismissWhenDragging || self.dismissButtonImage, @"You should set dismissButtonImage when shouldDismissWhenDragging is NO");
    
    if (self.shouldDismissWhenDragging == NO && CGPointEqualToPoint(CGPointZero, self.dismissButtonCenter))
    {
        NSLog(@"[DEBUG] Waring: Suggested setting KSGuaidManager.dismissButtonCenter values.");
    }
    
    if ([current_version() compare:prev_version()] == NSOrderedDescending)
    {
        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor clearColor];
        _window.windowLevel = UIWindowLevelStatusBar;
        [_window makeKeyAndVisible];
        
        EDSGuideViewController *rootVC = [[EDSGuideViewController alloc] init];
        __weak typeof(self)weakSelf = self;
        rootVC.willDismissHandler = ^{
            save_current_version();
            [weakSelf end];
        };
        _window.rootViewController = rootVC;
    }
    else
    {
        [self end];
    }
}


- (void)end
{
    _window.hidden = YES;
    _window = nil;
    _images = nil;
    _instance = nil;
}

NS_INLINE
NSString* current_version()
{
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *app_build = infoDict[@"CFBundleShortVersionString"];//infoDict[@"CFBundleVersion"];
    return [NSString stringWithFormat:@"%@",app_build];
}

NS_INLINE
NSString* prev_version()
{
    return [NSString stringWithContentsOfFile:version_path() encoding:NSUTF8StringEncoding error:nil];
}

NS_INLINE
void save_current_version()
{
    [current_version() writeToFile:version_path() atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

NS_INLINE
NSString* version_path()
{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Version.data"];;
}


@end











































